package com.imooc.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Created By Houce
 * @date 2020-07-16 15:27
 */
@Component
public class TestInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("==================进入拦截器preHandle==================");
        request.setAttribute("startTime",System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("==================处理完Controller方法进入拦截器postHandle==================");
        Long startTime = (Long)request.getAttribute("startTime");
        System.out.println("从进入preHandle到完成Controller方法，进入postHandle花费的时间是："+(System.currentTimeMillis()-startTime));
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        System.out.println("==================进入拦截器afterCompletion==================");
        Long startTime = (Long)request.getAttribute("startTime");
        System.out.println("从进入preHandle到完成Controller方法，再到进入postHandle，最后到afterCompletion花费的时间是："+(System.currentTimeMillis()-startTime));
    }
}
